package com.yix.admin.modules.system.entity;

import com.yix.common.base.BaseUUIDEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/1/20 17:21
 */
@Entity
@Getter
@Setter
@Table(name="sys_user")
@org.hibernate.annotations.Table(appliesTo = "sys_user",comment = "用户表")
public class SysUser extends BaseUUIDEntity implements Serializable {

    @ManyToMany
    @ApiModelProperty(value = "用户角色")
    @JoinTable(name = "users_roles",
            joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
    private Set<SysRole> roles;

    @ManyToMany
    @ApiModelProperty(value = "用户岗位")
    @JoinTable(name = "users_jobs",
            joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "job_id",referencedColumnName = "id")})
    private Set<SysJob> jobs;

    @OneToOne
    @JoinColumn(name = "dept_id")
    @ApiModelProperty(value = "用户部门")
    private SysDept dept;

    /**
     * 这个字段的值在这张表里不能重复
     */
    @NotBlank
    @Column(unique = true,columnDefinition = "varchar(300) COMMENT '登录名'")
    @ApiModelProperty(value = "登录名")
    private String username;

    @Column(columnDefinition = "varchar(500) COMMENT '密码'")
    @ApiModelProperty(value = "密码")
    private String password;

    @NotBlank
    @Column(columnDefinition = "varchar(300) COMMENT '用户昵称'")
    @ApiModelProperty(value = "用户昵称")
    private String nickName;

    @Column(columnDefinition = "varchar(10) COMMENT '性别'")
    @ApiModelProperty(value = "性别")
    private String gender;

    @NotNull
    @Column(columnDefinition = "tinyint(1) COMMENT '状态：1启用、0禁用'")
    @ApiModelProperty(value = "状态：1启用、0禁用")
    private Boolean enabled;

    @NotBlank
    @Column(columnDefinition = "varchar(40) COMMENT '手机号'")
    @ApiModelProperty(value = "手机号")
    private String phone;

    @NotBlank
    @Email
    @Column(columnDefinition = "varchar(40) COMMENT '邮件'")
    @ApiModelProperty(value = "邮件")
    private String email;

    @ApiModelProperty(value = "头像真实名称",hidden = true)
    private String avatarName;

    @ApiModelProperty(value = "头像存储的路径", hidden = true)
    private String avatarPath;

    @ApiModelProperty(value = "是否为admin账号", hidden = true)
    private Boolean isAdmin = false;

    @Column(name = "pwd_reset_time")
    @ApiModelProperty(value = "最后修改密码的时间", hidden = true)
    private Date pwdResetTime;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SysUser user = (SysUser) o;
        return Objects.equals(id, user.id) &&
                Objects.equals(username, user.username);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, username);
    }
}
